﻿namespace StreetCrime
{
    using System;
    using System.Diagnostics;
    using System.Reactive.Linq;
    using System.Reflection;
    using WP7Contrib.Caching;
    using WP7Contrib.Communications;

    using System.Collections.Generic;
    using System.Windows;
    using Microsoft.Phone.Controls;
    using WP7Contrib.Logging;

    public partial class MainPage : PhoneApplicationPage
    {
        private readonly IsolatedStorageCacheProvider cacheProvider;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            var assemblies = new List<Assembly>();
            assemblies.Add(this.GetType().Assembly);
            cacheProvider = new IsolatedStorageCacheProvider("StreetCrime", assemblies, new NullLoggingService());
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // need to use real values...
            var username = "username";
            var password = "password";

            var @lat = 52.629729;
            var @long = -1.131592;

            var url = "http://policeapi2.rkh.co.uk/api/crimes-street/all-crime?lat={0}&lng={1}";

            IHandleResources resourceClient = new ResourceClient();
            var service = new StreetLevelCrimeService(resourceClient,
                                                      cacheProvider,
                                                      new NullLoggingService(),
                                                      url,
                                                      username,
                                                      password);

            var criteria = new StreetLevelCrimeCriteria { Latitude = @lat, Longitude = @long };
            var tmp = service.Search(criteria);
            tmp.Subscribe(result =>
            {  
                var categories = result.GroupByCategory();
                var streets = result.GroupByStreet();

                var streetCategories = result.GroupByStreetAndCategory();

                foreach (var streetCategory in streetCategories)
                {
                    foreach (var streetCategoryCrimes in streetCategory.Crimes)
                    {
                        Debug.WriteLine("{0} - {1}, Count = {2}",
                                        streetCategory.Street.Name,
                                        streetCategoryCrimes.Category,
                                        streetCategoryCrimes.Crimes.Count);
                    }
                }

                Debug.WriteLine("***************************************");

                var streetFiltered = result.FilterByStreet("Rivers Street");
                foreach (var streetCategory in streetFiltered)
                {
                    Debug.WriteLine("{0} - {1}, Count = {2}",
                                    "Rivers Street",
                                    streetCategory.Category,
                                    streetCategory.Crimes.Count);
                }

                Debug.WriteLine("***************************************");

                var categoryFiltered = result.FilterByGroup(StreetLevelCrimeCategory.Vehicle);
                foreach (var streetCategory in categoryFiltered)
                {
                    Debug.WriteLine("{0} - {1}, Count = {2}",
                                    streetCategory.Street.Name,
                                    StreetLevelCrimeCategory.Vehicle,
                                    streetCategory.Crimes.Count);
                }

                Debug.WriteLine("***************************************");
            });
        }
    }
}